using System;
using Microsoft.Win32;
using System.Globalization;
using System.IO;

namespace Transferomatic
{
    public sealed class RegistryAgent
    {
        #region Constants
        private const string KEY_BUFFER_SIZE = "Buffer Size";
        private const string KEY_CONCURRENT_DOWNLOADS = "Concurrent Downloads";
        private const string KEY_LAST_FILE_TRANSFER_SOURCE_DIRECTORY = "Last File Transfer Source Directory";
        private const string KEY_LAST_FILE_TRANSFER_DESTINATION_DIRECTORY = "Last File Transfer Destination Directory";
        private const string KEY_LAST_DIRECTORY_TRANSFER_SOURCE_DIRECTORY = "Last Directory Transfer Source Directory";
        private const string KEY_LAST_DIRECTORY_TRANSFER_DESTINATION_DIRECTORY = "Last Directory Transfer Destination Directory";
        #endregion

        #region Properties
        public static RegistryKey SettingsKey
        {
            get
            {
                RegistryKey theSettingsKey =
                    Registry.LocalMachine.OpenSubKey("Software\\www.ne0ge0.com\\Transfer-o-matic", true);
                if (null == theSettingsKey)
                {
                    RegistryKey theSoftwareKey =
                        Registry.LocalMachine.OpenSubKey("Software", true);
                    RegistryKey theComputaKey =
                        theSoftwareKey.CreateSubKey("www.ne0ge0.com");
                    theSettingsKey =
                        theComputaKey.CreateSubKey("Transfer-o-matic");
                }
                return theSettingsKey;
            }
        }

        public static int BufferSize
        {
            get
            {
                if (null == RegistryAgent.SettingsKey.GetValue(RegistryAgent.KEY_BUFFER_SIZE))
                {
                    RegistryAgent.SettingsKey.SetValue(
                        RegistryAgent.KEY_BUFFER_SIZE,
                        32768);
                }
                return Convert.ToInt32(
                    RegistryAgent.SettingsKey.GetValue(RegistryAgent.KEY_BUFFER_SIZE),
                    CultureInfo.CurrentCulture);

            }
            set
            {
                RegistryAgent.SettingsKey.SetValue(RegistryAgent.KEY_BUFFER_SIZE, value);
            }
        }
        
        public static int ConcurrentDownloads
        {
            get
            {
                if (null == RegistryAgent.SettingsKey.GetValue(RegistryAgent.KEY_CONCURRENT_DOWNLOADS))
                {
                    RegistryAgent.SettingsKey.SetValue(
                        RegistryAgent.KEY_CONCURRENT_DOWNLOADS,
                        1);
                }
                return Convert.ToInt32(
                    RegistryAgent.SettingsKey.GetValue(RegistryAgent.KEY_CONCURRENT_DOWNLOADS),
                    CultureInfo.CurrentCulture);

            }
            set
            {
                RegistryAgent.SettingsKey.SetValue(RegistryAgent.KEY_CONCURRENT_DOWNLOADS, value);
            }
        }

        public static string LastFileTransferSourceDirectory
        {
            get
            {
                if (null == RegistryAgent.SettingsKey.GetValue(RegistryAgent.KEY_LAST_FILE_TRANSFER_SOURCE_DIRECTORY))
                {
                    RegistryAgent.SettingsKey.SetValue(
                        RegistryAgent.KEY_LAST_FILE_TRANSFER_SOURCE_DIRECTORY,
                        Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
                }
                return Convert.ToString(
                    RegistryAgent.SettingsKey.GetValue(RegistryAgent.KEY_LAST_FILE_TRANSFER_SOURCE_DIRECTORY),
                    CultureInfo.CurrentCulture);

            }
            set
            {
                RegistryAgent.SettingsKey.SetValue(RegistryAgent.KEY_LAST_FILE_TRANSFER_SOURCE_DIRECTORY, value);
            }
        }

        public static string LastFileTransferDestinationDirectory
        {
            get
            {
                if (null == RegistryAgent.SettingsKey.GetValue(RegistryAgent.KEY_LAST_FILE_TRANSFER_DESTINATION_DIRECTORY))
                {
                    RegistryAgent.SettingsKey.SetValue(
                        RegistryAgent.KEY_LAST_FILE_TRANSFER_DESTINATION_DIRECTORY,
                        Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
                }
                return Convert.ToString(
                    RegistryAgent.SettingsKey.GetValue(RegistryAgent.KEY_LAST_FILE_TRANSFER_DESTINATION_DIRECTORY),
                    CultureInfo.CurrentCulture);

            }
            set
            {
                RegistryAgent.SettingsKey.SetValue(RegistryAgent.KEY_LAST_FILE_TRANSFER_DESTINATION_DIRECTORY, value);
            }
        }

        public static string LastDirectoryTransferSourceDirectory
        {
            get
            {
                if (null == RegistryAgent.SettingsKey.GetValue(RegistryAgent.KEY_LAST_DIRECTORY_TRANSFER_SOURCE_DIRECTORY))
                {
                    RegistryAgent.SettingsKey.SetValue(
                        RegistryAgent.KEY_LAST_DIRECTORY_TRANSFER_SOURCE_DIRECTORY,
                        Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
                }
                return Convert.ToString(
                    RegistryAgent.SettingsKey.GetValue(RegistryAgent.KEY_LAST_DIRECTORY_TRANSFER_SOURCE_DIRECTORY),
                    CultureInfo.CurrentCulture);

            }
            set
            {
                RegistryAgent.SettingsKey.SetValue(RegistryAgent.KEY_LAST_DIRECTORY_TRANSFER_SOURCE_DIRECTORY, value);
            }
        }

        public static string LastDirectoryTransferDestinationDirectory
        {
            get
            {
                if (null == RegistryAgent.SettingsKey.GetValue(RegistryAgent.KEY_LAST_DIRECTORY_TRANSFER_DESTINATION_DIRECTORY))
                {
                    RegistryAgent.SettingsKey.SetValue(
                        RegistryAgent.KEY_LAST_DIRECTORY_TRANSFER_DESTINATION_DIRECTORY,
                        Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
                }
                return Convert.ToString(
                    RegistryAgent.SettingsKey.GetValue(RegistryAgent.KEY_LAST_DIRECTORY_TRANSFER_DESTINATION_DIRECTORY),
                    CultureInfo.CurrentCulture);

            }
            set
            {
                RegistryAgent.SettingsKey.SetValue(RegistryAgent.KEY_LAST_DIRECTORY_TRANSFER_DESTINATION_DIRECTORY, value);
            }
        }
        #endregion

        #region Methods
        private RegistryAgent() {}
        #endregion
    }
}
